const {allCityData} = require("./county_data")

let county_sum = []

for (let i = 0;i<allCityData.length;i++){

    let start = new Date("2022-01-01");
    let end = new Date("2022-12-31");
    let county_all_day = []
    //每个城市每一日的数据
    while(start <= end){
        let mm = ((start.getMonth()+1)>=10)?(start.getMonth()+1):'0'+(start.getMonth()+1);
        let dd = ((start.getDate())>=10)? (start.getDate()) : '0' + (start.getDate());
        let yyyy = start.getFullYear();
        let date = yyyy+"-"+mm+"-"+dd;

        let gender_number = Math.floor(Math.random()*100)

        let sales = [
            sales_pla(1130,1050,990),
            sales_pla(790,1110,1060),
            sales_pla(1330,1120,930),
            sales_pla(1120,950,900),
            sales_pla(950,1020,930)
        ]

        let sales_red_sum = 0
        let sales_green_sum = 0
        let sales_other_sum = 0
        for (let i = 0;i<sales.length;i++){
            sales_red_sum += sales[i].red_shell
            sales_green_sum += sales[i].green_shell
            sales_other_sum += sales[i].other
        }

        let county_day = {
            county:allCityData[i].name,
            city_code:allCityData[i].code,
            year:date,
            platform_classification:{
                platform_sales:[
                    {'京东':sales[0]},
                    {'淘宝':sales[1]},
                    {'拼多多':sales[2]},
                    {'盒马':sales[3]},
                    {'美团':sales[4]}
                ],
                gender: {'male':gender_number,'female':100-gender_number},
                age:getRandomNum()
            },
            sales:{
                sales_red_sum:sales_red_sum,
                sales_green_sum:sales_green_sum,
                sales_other_sum,sales_other_sum
            },
        }

        county_all_day.push(county_day)

        start = new Date(start.setDate(start.getDate() + 1));
    }


    // //每一整年的数据分析
    let red_sum = 0
    let green_sum = 0
    let other_sum = 0

    let jd_red_sum = 0
    let jd_green_sum = 0
    let jd_other_sum = 0
    let tb_red_sum = 0
    let tb_green_sum = 0
    let tb_other_sum = 0
    let pdd_red_sum = 0
    let pdd_green_sum = 0
    let pdd_other_sum = 0
    let hm_red_sum = 0
    let hm_green_sum = 0
    let hm_other_sum = 0
    let mt_red_sum = 0
    let mt_green_sum = 0
    let mt_other_sum = 0

    let male_sum = 0
    let age_18 = 0
    let age_28 = 0
    let age_38 = 0
    let age_48 = 0

    for (let i = 0; i < county_all_day.length; i++) {
        red_sum += county_all_day[i].sales.sales_red_sum
        green_sum += county_all_day[i].sales.sales_green_sum
        other_sum += county_all_day[i].sales.sales_other_sum

        jd_red_sum += county_all_day[i].platform_classification.platform_sales[0].京东.red_shell
        jd_green_sum += county_all_day[i].platform_classification.platform_sales[0].京东.green_shell
        jd_other_sum += county_all_day[i].platform_classification.platform_sales[0].京东.other
        tb_red_sum += county_all_day[i].platform_classification.platform_sales[1].淘宝.red_shell
        tb_green_sum += county_all_day[i].platform_classification.platform_sales[1].淘宝.green_shell
        tb_other_sum += county_all_day[i].platform_classification.platform_sales[1].淘宝.other
        pdd_red_sum += county_all_day[i].platform_classification.platform_sales[2].拼多多.red_shell
        pdd_green_sum += county_all_day[i].platform_classification.platform_sales[2].拼多多.green_shell
        pdd_other_sum += county_all_day[i].platform_classification.platform_sales[2].拼多多.other
        hm_red_sum += county_all_day[i].platform_classification.platform_sales[3].盒马.red_shell
        hm_green_sum += county_all_day[i].platform_classification.platform_sales[3].盒马.green_shell
        hm_other_sum += county_all_day[i].platform_classification.platform_sales[3].盒马.other
        mt_red_sum += county_all_day[i].platform_classification.platform_sales[4].美团.red_shell
        mt_green_sum += county_all_day[i].platform_classification.platform_sales[4].美团.green_shell
        mt_other_sum += county_all_day[i].platform_classification.platform_sales[4].美团.other

        male_sum += county_all_day[i].platform_classification.gender.male

        age_18 += Object.values(county_all_day[i].platform_classification.age[0])*1
        age_28 += Object.values(county_all_day[i].platform_classification.age[1])*1
        age_38 += Object.values(county_all_day[i].platform_classification.age[2])*1
        age_48 += Object.values(county_all_day[i].platform_classification.age[3])*1
    }

    let county = {
        county:allCityData[i].name,
        city_code:allCityData[i].code,
        year:'2022年',
        platform_classification:{
            platform_sales:[
                {'京东':{
                    'red_shell_sales':jd_red_sum,
                    'green_shell_sales':jd_green_sum,
                    'other_shell_sales':jd_other_sum
                    }},
                {'淘宝':{
                        'red_shell_sales':tb_red_sum,
                        'green_shell_sales':tb_green_sum,
                        'other_shell_sales':tb_other_sum
                    }},
                {'拼多多':{
                        'red_shell_sales':pdd_red_sum,
                        'green_shell_sales':pdd_green_sum,
                        'other_shell_sales':pdd_other_sum
                    }},
                {'盒马':{
                        'red_shell_sales':hm_red_sum,
                        'green_shell_sales':hm_green_sum,
                        'other_shell_sales':hm_other_sum
                    }},
                {'美团':{
                        'red_shell_sales':mt_red_sum,
                        'green_shell_sales':mt_green_sum,
                        'other_shell_sales':mt_other_sum
                    }},
            ],
            gender: {
                male:Math.round(male_sum/county_all_day.length),
                female:Math.round(100 - male_sum/county_all_day.length)
            },
            age:{
                '18-28':Math.round(age_18/county_all_day.length),
                '28-38':Math.round(age_28/county_all_day.length),
                '38-48':Math.round(age_38/county_all_day.length),
                '48-58':Math.round(age_48/county_all_day.length),
                '其他':100 - Math.round(age_18/county_all_day.length) -
                    Math.round(age_28/county_all_day.length) -
                    Math.round(age_38/county_all_day.length) -
                    Math.round(age_48/county_all_day.length),
                }
        },
        sales:{
            'sales_red_sum':red_sum,
            'sales_green_sum':green_sum,
            'sales_other_sum':other_sum
        },
        level:allCityData[i].level
    }

    county_sum.push(county)
}

function sales_pla(red,green,other){
    return {
        red_shell:Math.floor(Math.random()*red),
        green_shell:Math.floor(Math.random()*green),
        other:Math.floor(Math.random()*other)
    }
}

function getRandomNum() {
    let res = [];   //最后返回的数组
    let ages = ['18-28','28-38','38-48','48-58','其他']
    let range = 100;  //生成随机金额的范围
    let preTotal = 0;   //已经生成的金额的和
    for (let i = 0; i < (ages.length-1); i++) {
        let item = Math.ceil(Math.random() * range);
        res.push({[ages[i]]:item});
        range -= item;
        preTotal += item;
    }
    res.push({[ages.pop()]:100 - preTotal});
    return res;
}

module.exports = {county_sum}
